package com.lc.tool;

import android.content.Context;
import android.os.Looper;
import android.view.Gravity;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * Created by zhou-yb on 16/10/25.
 */

public class AppHelper {
    /// 返回app是否有root权限
    public static boolean HasSuperUser() {
        try {
            File file = new File("/system/xbin/su");
            if (file.exists()) {
                return true;
            }
        } catch (Throwable e1) {

        }
        try {
            File file = new File("/system/bin/su");
            if (file.exists()) {
                return true;
            }
        } catch (Throwable e1) {

        }

        return false;
    }

    /**
     * 以Root权限执行命令
     * @param cmd 需要以root权限执行的命令
     * @return 是否获取root权限
     */
    public static boolean UpgradeRootPermission(String cmd) {
        Process process = null;
        DataOutputStream os = null;
        try {
            process = Runtime.getRuntime().exec("su"); //切换到root帐号
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(cmd + "\n");
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
        } catch (Exception e) {
            return false;
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                process.destroy();
            } catch (Exception e) {
            }
        }
        return true;
    }

    /**
     * 执行命令
     * @param cmd 需要执行的命令
     */
    public static boolean OnCommand(String cmd) {
        try {
            String[] argv = cmd.split(" ");
            Process chmod = Runtime.getRuntime().exec(argv);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 气泡提示信息
     * @param context app Context
     * @param arg 参数
     * - 参数:
     *  - IsLooper : boolean [default:true] 是否启动消息循环
     *  - MSG : string 需要显示的信息
     *  - Mode : string 模式
     *   - SHORT : 短时间提示
     *   - LONG : 长时间提示
     *  .
     *  - Gravity : string [default:BOTTOM] 显示位置(TOP|CENTER|BOTTOM|RIGHT)
     *  - X : int [default:0] 显示的X坐标
     *  - Y : int [default:0] 显示的Y坐标
     * .
     */
    public static void Toast(Context context, JSONObject arg) {
        boolean isLooper = arg.optBoolean("IsLooper", true);
        if(isLooper) {
            Looper.prepare();
        }
        String msg = arg.optString("MSG");
        int mode = arg.optString("Mode", "SHORT").equalsIgnoreCase("LONG") ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(context, msg, mode);
        int gravity = Gravity.NO_GRAVITY;
        String sGravity = arg.optString("Gravity", "BOTTOM");
        if(sGravity.contains("TOP")) {
            gravity |= Gravity.TOP;
        }
        if(sGravity.contains("CENTER")) {
            gravity |= Gravity.CENTER;
        }
        if(sGravity.contains("BOTTOM")) {
            gravity |= Gravity.BOTTOM;
        }
        if(sGravity.contains("RIGHT")) {
            gravity |= Gravity.RIGHT;
        }
        int x = arg.optInt("X", 0);
        int y = arg.optInt("Y", 0);
        toast.setGravity(gravity, x, y);
        toast.show();
        if(isLooper) {
            Looper.loop();
        }
    }

    /**
     * 气泡提示
     */
    public static void Toast(Context context, String msg) {
        JSONObject arg = new JSONObject();
        try {
            arg.put("MSG", msg);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Toast(context, arg);
    }

    /**
     * 递归创建文件夹
     * @param file 需要创建的文件
     */
    public static void MakeDir(File file) {
        if(file.getParentFile().exists()){
            file.mkdir();
        }else{
            MakeDir(file.getParentFile());
            file.mkdir();
        }
    }

    /**
     * 读取整个文件数据到字符串中
     * @param path 需要读取的文件路径
     * @return 读取到的数据
     */
    public static String ReadText(String path) {
        File file = new File(path);
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStreamReader streamReader = new InputStreamReader(fileInputStream);
            BufferedReader reader = new BufferedReader(streamReader);
            char[] chars = new char[1024];
            StringBuilder builder = new StringBuilder();
            while(reader.read(chars)>=0){
                String value = new String(chars);
                builder.append(value);
            }
            return builder.toString().trim();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 将整个文本写入到文件中
     * @param path 需要写入的文件路径
     * @param text 需要写入的文本信息
     * @return 是否写入成功
     */
    public static boolean WriteText(String path, String text) {
        File file = new File(path);
        try {
            MakeDir(file.getParentFile());

            FileWriter fileWriter = new FileWriter(file);
            BufferedWriter writer = new BufferedWriter(fileWriter);
            writer.write(text);
            writer.flush();
            writer.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}